package cn.waka.sys.admin.controller;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import cn.waka.sys.admin.object.Channel;
import cn.waka.sys.admin.object.Merchant;
import cn.waka.sys.admin.object.Page;
import cn.waka.sys.admin.object.Result;
import cn.waka.sys.admin.service.AppService;
import cn.waka.sys.admin.service.ChannelService;
import cn.waka.sys.admin.service.MerchantService;
import cn.waka.sys.admin.utils.Validator;

@RestController
@RequestMapping("/channel")
public class ChannelController extends BaseController {


    private ChannelService channelService;

    private AppService appService;
    private MerchantService mchService;

    @Autowired
    public void setChannelService(ChannelService channelService) {
        this.channelService = channelService;
    }

    @Autowired
    public void setAppService(AppService appService) {
        this.appService = appService;
    }

    @Autowired
    public void setMchService(MerchantService mchService) {
        this.mchService = mchService;
    }

    @GetMapping(path = "/index", name = "渠道分页")
    public ModelAndView pageList(@RequestParam(name = "page", required = false, defaultValue = "1") int page, @RequestParam(name = "size", required = false, defaultValue = "10") int size, HttpServletRequest req, HttpServletResponse res) {
        Page p = new Page();
        p.setPage(page);
        p.setSize(size);

        ModelAndView mv = new ModelAndView();
        channelService.findPage(p);
        mv.addObject("page", p);
        return mv;

    }

    @RequestMapping(path = "/select_app", name = "选择应用")
    public ModelAndView selectApp() {
        return new ModelAndView("/channel/select_app");
    }

    @RequestMapping(path = "/select_mch", name = "选择商家")
    public ModelAndView selectMch() {
        return new ModelAndView("/channel/select_mch");
    }


    @RequestMapping(path = "/add", name = "添加渠道")
    public ModelAndView add(HttpServletRequest req, HttpServletResponse res) {
        Merchant obj = new Merchant();
        ModelAndView mv = new ModelAndView("/channel/edit");
        mv.addObject("vo", obj);
        return mv;
    }

    @RequestMapping(path = "/edit", name = "修改渠道")
    public ModelAndView edit(@RequestParam(name = "id", required = false) String idStr, HttpServletRequest req, HttpServletResponse res) {

        int id = 0;
        if (idStr != null && Validator.isNum(idStr)) {
            id = Integer.parseInt(idStr);
        }
        Map<String, Object> obj = null;
        if (id == 0) {
            obj = new HashMap<String, Object>();
        } else {
            obj = channelService.findById(id);
            if (obj == null) {
                obj = new HashMap<String, Object>();
            }
        }

        ModelAndView mv = new ModelAndView();
        mv.addObject("vo", obj);
        return mv;
    }


    @RequestMapping(path = "/save", name = "保存渠道")
    public @ResponseBody
    Result save(@RequestParam(name = "id", required = false) String idStr, @RequestParam(name = "name", required = false) String name, @RequestParam(name = "app_id", required = false) int appId, @RequestParam(name = "desc", required = false) String desc,
                @RequestParam(name = "mch_id", required = false) int operator, HttpServletRequest req, HttpServletResponse res, HttpSession sess) {
        int id = 0;
        if (idStr != null && Validator.isNum(idStr)) {
            id = Integer.parseInt(idStr);
        }
        if (name == null) {
            return error(0, "名称不能为空！");
        }
        if (appId == 0) {
            return error(0, "请选择应用！");
        }
        if (operator == 0) {
            return error(0, "请选择运营商！");
        }
        Channel o = new Channel();
        o.setId(id);
        o.setName(name);
        o.setDesc(desc);
        o.setAppId(appId);
        o.setOperator(operator);


        int rs = channelService.save(o);

        if (rs > 0) {
            return error(1, "保存成功！");
        } else {
            return error(0, "保存失败！");
        }
    }

    @RequestMapping(path = "/app/index", name = "选择应用分页")
    public @ResponseBody
    Map<String, Object> selectApp(@RequestParam(name = "page", required = false, defaultValue = "1") int page, @RequestParam(name = "limit", required = false, defaultValue = "10") int size, HttpServletRequest req, HttpServletResponse res) {
        Page p = new Page();
        p.setPage(page);
        p.setSize(size);
        appService.findPage(p);
        Map<String, Object> rs = new HashMap<String, Object>();
        rs.put("code", 0);
        rs.put("count", p.getCount());
        rs.put("data", p.getData());
        rs.put("msg", "");
        return rs;
    }

    @RequestMapping(path = "/mch/index", name = "选择商家分页")
    public @ResponseBody
    Map<String, Object> selectMch(@RequestParam(name = "page", required = false, defaultValue = "1") int page, @RequestParam(name = "limit", required = false, defaultValue = "10") int size, HttpServletRequest req, HttpServletResponse res) {
        Page p = new Page();
        p.setPage(page);
        p.setSize(size);
        mchService.findPage(p, 1);
        Map<String, Object> rs = new HashMap<String, Object>();
        rs.put("code", 0);
        rs.put("count", p.getCount());
        rs.put("data", p.getData());
        rs.put("msg", "");

        return rs;

    }

}
